package com.jie.deletefile.Contacts;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.jie.deletefile.Permission.CheckPermission;
import com.jie.deletefile.Permission.PermissionActivity;
import com.jie.deletefile.R;

public class ContactsActivity extends AppCompatActivity implements View.OnClickListener {

    private android.widget.Button btncontacts;
    private android.widget.RelativeLayout activitycantacts;
    private static final int REQUEST_CODE = 1;
    private static final String[] PERMISSION = new String[]{
            Manifest.permission.READ_CONTACTS,
            Manifest.permission.WRITE_CONTACTS,
    };
    private boolean hadPermission;
    private CheckPermission checkPermission;
    private String TAG = "TAG";
    private static final int FIRST=1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e(TAG, "onCreate: 111111" );
        setContentView(R.layout.activity_cantacts);
        this.activitycantacts = (RelativeLayout) findViewById(R.id.activity_cantacts);
        this.btncontacts = (Button) findViewById(R.id.btn_contacts);
        btncontacts.setOnClickListener(this);
        checkPermission = new CheckPermission(this);
        if (checkPermission.permissionSet(PERMISSION)) {
            startPermissionActivity();
            hadPermission = false;
        } else {
            hadPermission = true;
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
//            if (checkPermission.permissionSet(PERMISSION)) {
//                startPermissionActivity();
//                hadPermission = false;
//            } else {
//                hadPermission = true;
//            }




    }

    private void startPermissionActivity() {
        PermissionActivity.startActivityForResult(ContactsActivity.this, REQUEST_CODE, PERMISSION);
    }

    @Override
    public void onClick(View v) {
        if (!hadPermission) {
            Toast.makeText(this, "缺少权限", Toast.LENGTH_SHORT).show();
            return;
        }
        Intent intent = new Intent(ContactsActivity.this, ShowActivity.class);
        startActivity(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.e(TAG, "onActivityResult: 11111");
        if (requestCode == REQUEST_CODE && requestCode == PermissionActivity.PERMISSION_DENIEG) {
            hadPermission = false;
        } else {
            hadPermission = true;
        }

    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.e(TAG, "onStop: 111111" );
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.e(TAG, "onRestart: 111111" );
    }

}
